package cn.edu.xmu.orderpayment.payment.microservice.wechatpayvo;

import cn.edu.xmu.orderpayment.payment.util.baseobject.PaymentInfo;
import cn.edu.xmu.orderpayment.payment.util.retvo2baseobj.PaymentQuery;
import cn.edu.xmu.orderpayment.payment.model.bo.Payment;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * @author ziyi guo
 * @date 2021/11/30
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class WeChatPayTransactionRetVo implements PaymentQuery {




    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PayerRetVo{
        private String openid;
    }

    private String appid;

    private String mchid;

    private String outTradeNo;

    private String transactionId;

    private String tradeType;


    //SUCCESS：支付成功
    //REFUND：转入退款
    //NOTPAY：未支付
    //CLOSED：已关闭
    //REVOKED：已撤销（仅付款码支付会返回）
    //USERPAYING：用户支付中（仅付款码支付会返回）
    //PAYERROR：支付失败（仅付款码支付会返回）

    private String tradeState;

    private String tradeStateDesc;

    private TransactionAmountRetVo amount;

    private PayerRetVo payer;

    @JsonFormat(shape = JsonFormat.Shape.STRING,pattern ="yyyy-MM-dd'T'HH:mm:ss.SSS" ,timezone = "GMT+8")
    private LocalDateTime successTime;


    @Override
    public PaymentInfo createPaymentInfo() {
        PaymentInfo paymentInfo =new PaymentInfo();
        paymentInfo.setAmount(Long.valueOf(amount.getTotal()==null?0:amount.getTotal()));
        paymentInfo.setOutTradeNo(outTradeNo);
        paymentInfo.setRealAmount(Long.valueOf(amount.getPayerTotal()==null?0:amount.getPayerTotal()));
        paymentInfo.setTradeSn(transactionId);
        paymentInfo.setSuccessTime(successTime);

        switch (tradeState){
            case "SUCCESS":
            case "REFUND":
            case "USERPAYING":
                paymentInfo.setState(Payment.State.HAS_PAYED);
                break;
            case "NOTPAY":
                paymentInfo.setState(Payment.State.FAILED_PAY);
                break;
            case "CLOSED":
            case "REVOKED":
                paymentInfo.setState(Payment.State.CANCELED);
                break;
            case "PAYERROR":
                paymentInfo.setState(Payment.State.FAILED_PAY);
                break;
        }

        return paymentInfo;
    }



}
